package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.adapter.HolidayLeaveTypeAdapter;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.http.ApiRequest;

public class HolidayLeaveTypeDialogFragment extends BaseProgressBottomDialogFragment implements OnItemClickListener {

	@Bind(R.id.id_listview)
	protected ListView listview;

	private HolidayLeaveTypeAdapter adapter;

	private List<ArrangeType> list = new ArrayList<ArrangeType>();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//无标题
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_holiday_leave_type_dialog;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {
		listview.setOnItemClickListener(this);
	}

	@Override
	protected void initData() {

		//获得排班分类信息
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
		if (tokenKeeper.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("circle_id", tokenKeeper.getUser().circleId());
			params.put("work_type", 1);//请假列表

			ApiRequest.holidayLeaveTypeList.request(this, params, false);

		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		//选择
		adapter.getCheck().clear();
		adapter.check(position);
		
		//赋值
		List<Integer> checkList = adapter.getCheck();
		ArrangeType arrangeType = list.get(checkList.iterator().next());
		HolidayLeaveAddFragment frag = (HolidayLeaveAddFragment) getParentFragment();
		frag.setArrangeType(arrangeType);

		//隐藏对话框
		getDialog().dismiss();

	}

	@OnClick(R.id.id_holiday_type_btn)
	public void clickAddTypeBtn() {
		
		HolidayTypeAddFragment.go(getActivity());
		getDialog().dismiss();
		
	}

	public ListView getListview() {
		return listview;
	}

	public List<ArrangeType> getList() {
		return list;
	}

	public void setAdapter(HolidayLeaveTypeAdapter adapter) {
		this.adapter = adapter;
	}

	public HolidayLeaveTypeAdapter getAdapter() {
		return adapter;
	}

}